package com.wwdx.controller.rest;

import com.google.common.collect.Maps;
import com.wwdx.controller.base.BaseController;
import com.wwdx.model.WwdxCommentModel;
import com.wwdx.model.dto.ResultBean;
import com.wwdx.utils.ConstantHolder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Date;
import java.util.Map;

/**
 * @author liguoliang
 * @date 2018/5/23 18:54
 */
@RestController
@RequestMapping("/product/comment")
@ApiIgnore
public class RestCommentController extends BaseController{

    /**
     * 评论列表
     */
    @RequestMapping("/list")
    public ResultBean list(WwdxCommentModel model,
                           @RequestParam(value = "page",defaultValue = "1") int page,
                           @RequestParam(value = "rows",defaultValue = "10") int rows){

        model.setRest(true);
        Map<String, Object> map = Maps.newHashMapWithExpectedSize(2);
        map.put("count",wwdxCommentService.selectModelCount(model));
        map.put("list",wwdxCommentService.selectModelList(model, page, rows));
        return renderSuccess(map);
    }

    /**
     * 添加评论
     */
    @RequestMapping("/add")
    public ResultBean add(WwdxCommentModel model){
        model.setStatus(0);
        model.setPid(0L);
        model.setRecommend(0);
        model.setGmtCreated(new Date());
        wwdxCommentService.save(model);
        return renderSuccess();
    }

    /**
     * 点赞
     * @param id 评论ID 书籍ID 主题ID
     * @param userid 点赞人
     */
    @RequestMapping("/like")
    public ResultBean like(Long id,Long userid){
        String likeKey = String.format(ConstantHolder.LIKE_USER_SET, id);
        Boolean sismember = setOperations.isMember(likeKey, userid.toString());
        if (sismember){
            setOperations.remove(likeKey,userid.toString());
        }else {
            setOperations.add(likeKey,userid.toString());
        }
        return renderSuccess();
    }

    /**
     * 获取点赞数
     * @param id 产品ID
     * @param userid 点赞人
     */
    @RequestMapping("/getLikeCount")
    public ResultBean getLikeCount(Long id,Long userid){
        Map<String, Object> map = Maps.newHashMapWithExpectedSize(2);
        String likeKey = String.format(ConstantHolder.LIKE_USER_SET, id);
        map.put("count", setOperations.size(likeKey));
        map.put("islike", setOperations.isMember(likeKey, userid.toString()));
        return renderSuccess(map);
    }
}